package frontend.pages.reports.commuter;

import frontend.model.reports.CommutersReportEntry;
import frontend.model.reports.ReportInputUIModel;
import frontend.pages.reports.base.AbstractReportForm;
import frontend.pages.reports.base.AbstractReportPanel;
import frontend.reports.XLSFileRowCreator;
import shared.util.CalendarUtils;
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import java.util.*;

public class CommutersReportPanel extends AbstractReportPanel<CommutersReportEntry> {

    private static String RESOURCE_KEY_REPORT_FILE_NAME = "report.fileName.commuters";
    private ReportInputUIModel uiModel;
    private CommutersReportTableTemplate tableTemplate;

    public CommutersReportPanel(String id) {
        super(id);
    }

    @Override
    protected boolean shouldAddTotalHours() {
        return false;
    }

    @Override
    protected Component createTotalHoursPanel() {
        return null;
    }

    @Override
    protected DefaultDataTable<CommutersReportEntry> getReportResultsTable() {
        tableTemplate = new CommutersReportTableTemplate();
        return new CommutersReportTable("reportResultsTable", reportResults, tableTemplate);
    }

    @Override
    protected AbstractReportForm getReportForm() {
        uiModel = new ReportInputUIModel();
        return new CommutersReportForm("reportForm", uiModel, reportResults, reportResultsContainer);
    }

    @Override
    protected List<String> getReportFileHeader() {
        return tableTemplate.getHeader();
    }

    @Override
    protected List<Object[]> getReportFileData() {
        List<Object[]> reportFileData = new LinkedList<Object[]>();
        for (CommutersReportEntry reportEntry : reportResults) {
            XLSFileRowCreator rowCreator = new XLSFileRowCreator(4);
            Object[] row = rowCreator.newRow();
            rowCreator.addStringCell(row, 0, reportEntry.getIdentificationCard());
            rowCreator.addDBModelCell(row, 1, reportEntry.getEmployee());
            rowCreator.addHoursCell(row, 2, reportEntry.getDaysWithNormalHours());
            rowCreator.addHoursCell(row, 3, reportEntry.getDaysWithOvertime());
            reportFileData.add(row);
        }
        return reportFileData;
    }

    @Override
    protected Map<String, String> getReportDetails() {
        Map<String, String> reportDetails = super.getReportDetails();
        if (uiModel != null) {
            reportDetails.put(Localizer.get().getString(AbstractReportForm.RESOURCE_KEY_FIELD_LABEL_START_DATE, null),
                    CalendarUtils.formatDate(uiModel.getStartDate()));
            reportDetails.put(Localizer.get().getString(AbstractReportForm.RESOURCE_KEY_FIELD_LABEL_END_DATE, null),
                    CalendarUtils.formatDate(uiModel.getEndDate()));
        }
        return reportDetails;
    }

    @Override
    protected String getReportFileName() {
        return Localizer.get().getString(RESOURCE_KEY_REPORT_FILE_NAME, null);
    }

    @Override
    protected String getReportName() {
        return Localizer.get().getString("menu.reports.items.commuters", null);
    }


}

class CommutersReportTable extends DefaultDataTable<CommutersReportEntry> {

    private List<CommutersReportEntry> entriesList = new ArrayList<>();

    public CommutersReportTable(String id, List<CommutersReportEntry> entriesList, CommutersReportTableTemplate tableTemplate) {
        super(id, tableTemplate.getColumns(), getDataProvider(entriesList), 20);
        this.entriesList = entriesList;
    }

    public static ISortableDataProvider<CommutersReportEntry> getDataProvider(final List<CommutersReportEntry> list) {

        ISortableDataProvider<CommutersReportEntry> dataProvider = new SortableDataProvider<CommutersReportEntry>() {
            @Override
            public Iterator<? extends CommutersReportEntry> iterator(int first, int count) {
                return list.subList(first, first+count).iterator();
            }

            @Override
            public int size() {
                return list.size();
            }

            @Override
            public IModel<CommutersReportEntry> model(final CommutersReportEntry commutersReportEntry) {
                return new LoadableDetachableModel<CommutersReportEntry>() {
                    @Override
                    protected CommutersReportEntry load() {
                        return commutersReportEntry;
                    }
                };
            }
        };
        return dataProvider;
    }

    public boolean isVisible() {
        return this.entriesList.size() > 0;
    }

}
